package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {

        //1.
        Page<Setmeal> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        page = setmealMapper.selectPage(page,wrapper);

        List<Setmeal> setmealList = page.getRecords();
        if(CollectionUtil.isNotEmpty(setmealList)){
            for (Setmeal setmeal : setmealList) {
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
            }
        }
        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        //1.将基本信息保存到setmeal表
        setmealMapper.insert(setmeal);

        //2.遍历前段传入的菜品信息
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if(CollectionUtil.isNotEmpty(setmealDishList)){
            for (SetmealDish setmealDish : setmealDishList) {
                //3. 设置上套餐id
                setmealDish.setSetmealId(setmeal.getId());

                //将套餐菜品保存到setmeal_dish表
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //1. 删除套餐之前,要先去判断当前要删除的这一批套餐是否有在售状态,一旦有一个在售,整个删除失败
    //2. 删除套餐的时候,要将中间表的当前套餐相关数据一并删除
    @Override
    public void deleteByIds(Long[] ids) {
        //select count(0) from setmeal where status = 1 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids)
                .eq(Setmeal::getStatus,1);//在售
        Integer count = setmealMapper.selectCount(wrapper);
        if(count > 0){
            throw new CustomException("有在售套餐,无法删除");
        }

        //2. 删除套餐的时候,要将中间表的当前套餐相关数据一并删除
        //2-1 删除中间表
        //delete from setmeal_dish where setmeal_id in #{ids}
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishMapper.delete(wrapper1);

        //2-2 删除套餐表
        //delete from setmeal where status = 0 and id in #{ids}
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(Setmeal::getStatus,0)//套餐不在售
                .in(Setmeal::getId,ids);
        setmealMapper.delete(wrapper2);
    }

    @Override
    public Setmeal findById(Long id) {
        //1.根据id查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);

        //2. 根据套餐的分类id从分类表中查询分类信息(名称)
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        //3. 根据套餐id从菜品表查询当前菜品列表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);

        return setmeal;
    }

    @Override
    public void update(Setmeal setmeal) {
        //1.根据套餐id向套餐表更新套餐信息
        setmealMapper.updateById(setmeal);

        //2. 删除菜品表中当前套餐的相关信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //3. 将修改之后菜品列表重新保存到菜品表中
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if(CollectionUtil.isNotEmpty(setmealDishList)){
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());//设置套餐id
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    @Override
    public void updateStatus(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public List<Setmeal> findByCategoryId(Long categoryId, Integer status) {
        //select * from setmeal where category_id=#{} and status = #{}
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId)
                .eq(Setmeal::getStatus,status);
        return setmealMapper.selectList(wrapper);
    }

    @Override
    public List<Dish> getmealDish(Long id) {
        //1. 从中间表获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //2. 创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();

        //3. 遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {
            //根据菜品id从菜品表查询菜品详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList.add(dish);//添加到返回集合
        }

        //4. 返回
        return dishList;
    }

}
